//
//  HXCPrepareLessonsViewController.m
//  ACMediaFrameExample
//
//  Created by 付顺龙 on 2019/3/11.
//  Copyright © 2019 ArthurCao. All rights reserved.
//

#import "HXCPrepareLessonsViewController.h"

#import "HXCPrepareLessonsCollectionViewCell.h"

#define SidesOfWidth   10             //collectionView离两侧的宽度
#define CollectionSectionHeight 44.0
#define RowCount    2

@interface HXCPrepareLessonsViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *arr;
@property (nonatomic,assign) NSInteger selectIdx;
@end

@implementation HXCPrepareLessonsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr = [NSMutableArray array];
//    self.navigationController.view.backgroundColor = [UIColor whiteColor];
    self.selectIdx = -1;
    
    CGFloat Y = [UIApplication sharedApplication].statusBarFrame.size.height + 44;
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(14, Y+5-44, 60, 44);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelBtn];
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectMake(ScreenW -  60 - 14*2, Y+5-44, 60, 44);
    [confirmBtn setTitle:@"完成" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(finished) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmBtn];
    CGRect tFrame = CGRectMake(0, Y, ScreenW , self.view.frame.size.height - Y);
    UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
    fl.itemSize = CGSizeMake((ScreenW - SidesOfWidth)/2, (ScreenW - SidesOfWidth)/2);
    fl.minimumLineSpacing = 2.0;
    fl.minimumInteritemSpacing = 2.0;
    fl.sectionInset = UIEdgeInsetsMake(3, 3, 3, 3);
    fl.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc]initWithFrame:tFrame collectionViewLayout:fl];
    self.collectionView.backgroundColor = [UIColor lightGrayColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"HXCPrepareLessonsCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"HXCPrepareLessonsCollectionViewCell"];
    [self.view addSubview:self.collectionView];
    [self loadData];
}
- (void)loadData {
    NSString *token = [UserModel getUserModel].token;
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    [data setObject:self.hourId forKey:@"hourId"];
    [data setObject:token forKey:@"token"];
    [WYHXNetWorkManager get:WKQueryCourseHourImage_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
//        NSLog(@"%@",responseObj);
        NSString *code = responseObj[@"code"];
        NSString *msg = responseObj[@"msg"];
        if ([code integerValue] == 200) {
            self.arr = responseObj[@"data"];
            [self.collectionView reloadData];
        }else if ([code integerValue]==500){
            [MBProgressHUD showSuccess:msg toView:self.view];
            return ;
        }
    }];
}
- (void)cancelAction {
    [self dismissViewControllerAnimated:YES completion:nil];
};
-(void)finished {
    [self dismissViewControllerAnimated:YES completion:^{
//        NSLog(@"完成");
        if (self.block) {
            if (self.arr.count==0) {
                return ;
            }
            HXCPrepareLessonsCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"HXCPrepareLessonsCollectionViewCell" forIndexPath:[NSIndexPath indexPathForRow:self.selectIdx inSection:0]];
            [cell.contentIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",Public_URL,self.arr[self.selectIdx]]]];
            self.block(cell.contentIcon.image, self.arr[self.selectIdx]);
        }
    }];
}
#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource --
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [_arr count];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HXCPrepareLessonsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"HXCPrepareLessonsCollectionViewCell" forIndexPath:indexPath];
    [cell.contentIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",Public_URL,self.arr[indexPath.row]]]];
    if (indexPath.row == self.selectIdx) {
        cell.selectionIcon.hidden = NO;
    }else {
        cell.selectionIcon.hidden = YES;
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectIdx = indexPath.row;
    [collectionView reloadData];
}

@end
